package com.github.dice.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.dice.constant.ErrorEnums;
import com.github.dice.dto.JoinRoomDTO;
import com.github.dice.dto.ResultDTO;
import com.github.dice.dto.RollDiceDTO;
import com.github.dice.dto.RoomDTO;
import com.github.dice.entity.Player;
import com.github.dice.service.IPlayerService;
import com.github.dice.service.IRoomService;

@RestController
@RequestMapping("/player")
public class PlayerController extends BaseController {

    @Resource
    private IPlayerService playerService;

    @Resource
    private IRoomService   roomService;

    @PostMapping("/playerLogin")
    public ResultDTO playerLogin(@RequestParam(value = "playerName", required = false) String playerName,
                                 @RequestParam(value = "playerPwd", required = false) String playerPwd,
                                 HttpServletRequest request) {
        Player player = playerService.getPlayByNameAndPwd(playerName, playerPwd);
        if (!ObjectUtils.isEmpty(player)) {
            super.setPlayerSession(request, player);
            return new ResultDTO<>(player);
        } else {
            return new ResultDTO(ErrorEnums.E_5001);
        }
    }

    @PostMapping("/getPlayerBySession")
    public ResultDTO getPlayerBySession(@RequestParam("userName") String userName, HttpServletRequest request) {
        Player player = super.getPlayerBySession(request);
        if (ObjectUtils.isEmpty(player)) {
            return new ResultDTO<>(ErrorEnums.E_5002);
        } else {
            if (player.getPlayerName().equals(userName)) {
                return new ResultDTO<>(player);
            } else {
                return new ResultDTO<>(ErrorEnums.E_5002);
            }
        }
    }

    @PostMapping("/joinRoom")
    public ResultDTO joinRoom(@RequestBody JoinRoomDTO joinRoomDTO, HttpServletRequest request) {
        Player player = super.getPlayerBySession(request);
        if (null != player) {
            joinRoomDTO.setPlayerName(player.getPlayerName());
            RoomDTO roomDTO = roomService.joinRoom(joinRoomDTO);
            return new ResultDTO<>(roomDTO);
        } else {
            return new ResultDTO<>(ErrorEnums.E_5002);
        }
    }

    @PostMapping("/rollDice")
    public ResultDTO rollDice(@RequestBody RollDiceDTO rollDiceDTO, HttpServletRequest request) {
        Player player = super.getPlayerBySession(request);
        rollDiceDTO.setPlayerName(player.getPlayerName());
        playerService.rollDice(rollDiceDTO);
        return new ResultDTO();
    }

    @PostMapping("/addPlayer")
    public ResultDTO addPlayer(@RequestBody Player player, HttpServletRequest request) {
        playerService.addPlayer(player);
        if (!ObjectUtils.isEmpty(player)) {
            super.setPlayerSession(request, player);
            player.setPlayerPwd(StringUtils.EMPTY);
            return new ResultDTO<>(player);
        } else {
            return new ResultDTO(ErrorEnums.E_5003);
        }
    }

}
